package com.baolai.controller;

import com.alibaba.fastjson.JSONObject;
import com.baolai.entity.ShelfGoods;
import com.baolai.result.Result;
import com.baolai.result.ResultUtil;
import com.baolai.service.ShelfGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "货架上的备件")
@ResponseBody
@RestController
@RequestMapping("/shelf-goods")
public class ShelfGoodsController {

    @Autowired
    ShelfGoodsService shelfGoodsService;

    @ApiOperation(value = "入库")
    @PostMapping("inStock")
    public Result inStock(@RequestBody JSONObject jsonObject){
        return shelfGoodsService.inStock(jsonObject.getString("goodsId"),jsonObject.getInteger("shelfId"),
                jsonObject.getInteger("floor"),jsonObject.getString("siteId"),jsonObject.getInteger("number"),
                jsonObject.getString("account"),jsonObject.getString("name"));
    }

    @ApiOperation(value = "添加货架上的备件")
    @PostMapping("add")
    public Result addShelfGoods(@RequestBody ShelfGoods shelfGoods){
        if(shelfGoodsService.addShelfGoods(shelfGoods)){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.error("添加失败");
    }

    @ApiOperation(value = "根据备件编码和备件在货架上的位置查询")
    @PostMapping("get")
    public Result getShelfGoods(@RequestBody JSONObject jsonObject){
        return ResultUtil.success(shelfGoodsService.getGoodsShelf(jsonObject.getString("goodsId"),jsonObject.getString("site")));
    }

    @ApiOperation(value = "更新该备件在该位置的数量")
    @PostMapping("update")
    public Result updateShelfGoods(@RequestBody ShelfGoods shelfGoods){
        if(shelfGoodsService.updateShelfGoods(shelfGoods)){
            return ResultUtil.success("修改成功");
        }
        return ResultUtil.error("修改失败");
    }

    @ApiOperation(value = "出库")
    @PostMapping("outStock")
    public Result outStock(@RequestBody JSONObject jsonObject){
        return shelfGoodsService.outStock(jsonObject.getString("goodsId"),jsonObject.getString("site"),
                jsonObject.getString("account"),jsonObject.getString("name"),jsonObject.getInteger("number"));
    }

    @ApiOperation(value = "分页查询全部货架上的备件")
    @PostMapping("list")
    public Result listShelfGoods(@RequestBody JSONObject jsonObject){
        return ResultUtil.success(shelfGoodsService.listShelfGoods(jsonObject.getInteger("current"),jsonObject.getInteger("size")));
    }

    @ApiOperation(value = "根据备件名称的关键字分页查询货架上的备件")
    @PostMapping("query")
    public Result queryShelfGoods(@RequestBody JSONObject jsonObject){
        return ResultUtil.success(shelfGoodsService.queryShelfGoods(jsonObject.getString("key"),jsonObject.getInteger("current"),jsonObject.getInteger("size")));
    }

    @ApiOperation(value = "根据货架编号分页查询货架上的备件")
    @PostMapping("queryBySite")
    public Result queryShelfGoodsBySite(@RequestBody JSONObject jsonObject){
        return ResultUtil.success(shelfGoodsService.queryShelfGoodsBySite(jsonObject.getInteger("shelfId"),jsonObject.getInteger("current"),jsonObject.getInteger("size")));
    }



}
